package com.acme.bankapp.domain.bank;

import static java.lang.Math.round;

public abstract class AbstractAccount implements Account {

	protected int id;
	protected double balance;

	public AbstractAccount(final double amount) throws IllegalArgumentException {
		if (amount < 0) {
            throw new IllegalArgumentException("Negative balance");
        }
        this.balance = amount;

	}

    public long decimalValue(){
        return round(getBalance());
    }

	public double getBalance() {
		return balance;
	}

    @Override
    public void deposit(final double amount) {
        balance += amount;
    }

    public abstract void withdraw(final double amount) throws NotEnoughFundsException;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AbstractAccount)) return false;

        AbstractAccount that = (AbstractAccount) o;

        if (Double.compare(that.balance, balance) != 0) return false;
        if (id != that.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id;
        temp = Double.doubleToLongBits(balance);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;

    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", balance=" + balance +
                '}';
    }

}
